/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.probe;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.ProbeCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.engine.probe.ProbeStatusManager;
import com.sinodata.bsm.common.constants.SystemStatus;
import com.sinodata.bsm.common.vo.Probe;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-6 上午10:42:40          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ProbeService {

    private static final String CICP = "CICP"; //正常采集用

    private static final String ITIP = "ITIP"; //集成Tivoli用

    private static final String INMP = "INMP"; //集成朗逸网络资源用

    @Autowired
    private ProbeCache probeCache;

    @Autowired
    private BaseDao baseDao;

    /**
     * 
     */
    public ProbeService() {
    }

    /**
     * 获取所有的采集器  包括cicp和itip
     * @return
     */
    public List<Probe> findAllProbe() {
        return probeCache.getAll();
    }

    /**
     * 获取所有的CICP采集器
     * @return
     */
    public List<Probe> findAllCicpProbe() {
        List<Probe> list = new ArrayList<Probe>();
        for (Probe b : probeCache.getAll()) {
            if (CICP.equals(b.getType())) {
                list.add(b);
            }
        }
        return list;
    }

    /**
     * 获取所有的ITIP采集器
     * @return
     */
    public List<Probe> findAllItipProbe() {
        List<Probe> list = new ArrayList<Probe>();
        for (Probe b : probeCache.getAll()) {
            if (ITIP.equals(b.getType())) {
                list.add(b);
            }
        }
        return list;
    }

    /**
     * 获取所有的INMP采集器
     * @return
     */
    public List<Probe> findAllInmpProbe() {
        List<Probe> list = new ArrayList<Probe>();
        for (Probe b : probeCache.getAll()) {
            if (INMP.equals(b.getType())) {
                list.add(b);
            }
        }
        return list;
    }

    /**
     * 获取一个采集器 
     * @param probeId 采集器ID
     * @return
     */
    public Probe findById(Long probeId) {
        return probeCache.get(probeId);
    }

    /**
     * 停止一个采集器 
     * @param probeId  采集器ID
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void stopProbe(Long probeId) {
        ProbeStatusManager.getInstance().stopProbe(probeId);
        Probe probe = probeCache.get(probeId);
        probe.setStatus(SystemStatus.STATUS_DOWN);
        baseDao.saveOrUpdate(probe);
    }

    /**
     * 更新一个采集器 
     * @param probe
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateProbe(Probe probe) {
        baseDao.saveOrUpdate(probe);
    }

    /**
     * 停止所有的采集器
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void stopAllProbe() {
        List<Probe> probes = probeCache.getAll();
        for (Probe probe : probes) {
            stopProbe(probe.getId());
        }
    }
}
